package com.ywgt.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ywgt.system.mapper.LogMapper;
import com.ywgt.system.pojo.Log;
import com.ywgt.system.pojo.LoginLog;
import com.ywgt.system.mapper.LoginLogMapper;
import com.ywgt.system.service.LoginLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 登录日志表 服务实现类
 * </p>
 *
 * @author yiping
 * @since 2021-01-22
 */
@Service
public class LoginLogServiceImpl extends ServiceImpl<LoginLogMapper, LoginLog> implements LoginLogService {

    @Autowired
    private LoginLogMapper logMapper;
    @Override
    public Page<LoginLog> findLoginLogList(Long pageNum, Long pageSize, LoginLog loginLog) {
        Page<LoginLog> loginLogPage=new Page<>(pageNum,pageSize);
        return logMapper.selectPage(loginLogPage,getWrapper(loginLog));
    }

    @Override
    public List<Map<String, Object>> getReport(String username) {
        return logMapper.getReport(username);
    }


    public LambdaQueryWrapper<LoginLog> getWrapper(LoginLog loginLog){
        LambdaQueryWrapper<LoginLog> queryWrapper=new LambdaQueryWrapper<>();
        if (log!=null) {
            if (!StringUtils.isEmpty(loginLog.getLocation())) {
                queryWrapper.eq(LoginLog::getLocation, loginLog.getLocation());
            }
            if (!StringUtils.isEmpty(loginLog.getId())) {
                queryWrapper.eq(LoginLog::getIp, loginLog.getIp());
            }
            if (!StringUtils.isEmpty(loginLog.getUsername())) {
                queryWrapper.like(LoginLog::getUsername, loginLog.getUsername());
            }
        }
        return queryWrapper;
    }
}
